package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/request"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
	"gitee.com/g-qs/gin-vue-admin/server/service"
	"gitee.com/g-qs/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type HealthQAndAApi struct {
}

var QuestionAndAnswerService = service.ServiceGroupApp.HealthServiceGroup.HealthQAndAService

// CreateHealthQAndA 创建健康问答表
// @Tags HealthQAndA
// @Summary 创建健康问答表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQAndA true "创建健康问答表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /QuestionAndAnswer/createHealthQAndA [post]
func (QuestionAndAnswerApi *HealthQAndAApi) CreateHealthQAndA(c *gin.Context) {
	var QuestionAndAnswer health.HealthQAndA
	err := c.ShouldBindJSON(&QuestionAndAnswer)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Question": {utils.NotEmpty()},
		"Answer":   {utils.NotEmpty()},
	}
	if err := utils.Verify(QuestionAndAnswer, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	claims, _ := utils.GetClaims(c)
	//UserID := int(claims.BaseClaims.ID)
	//QuestionAndAnswer.Author = &UserID
	QuestionAndAnswer.Author = claims.BaseClaims.ID
	if err := QuestionAndAnswerService.CreateHealthQAndA(&QuestionAndAnswer); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteHealthQAndA 删除健康问答表
// @Tags HealthQAndA
// @Summary 删除健康问答表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQAndA true "删除健康问答表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /QuestionAndAnswer/deleteHealthQAndA [delete]
func (QuestionAndAnswerApi *HealthQAndAApi) DeleteHealthQAndA(c *gin.Context) {
	var QuestionAndAnswer health.HealthQAndA
	err := c.ShouldBindJSON(&QuestionAndAnswer)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := QuestionAndAnswerService.DeleteHealthQAndA(QuestionAndAnswer); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteHealthQAndAByIds 批量删除健康问答表
// @Tags HealthQAndA
// @Summary 批量删除健康问答表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除健康问答表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /QuestionAndAnswer/deleteHealthQAndAByIds [delete]
func (QuestionAndAnswerApi *HealthQAndAApi) DeleteHealthQAndAByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := QuestionAndAnswerService.DeleteHealthQAndAByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateHealthQAndA 更新健康问答表
// @Tags HealthQAndA
// @Summary 更新健康问答表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body health.HealthQAndA true "更新健康问答表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /QuestionAndAnswer/updateHealthQAndA [put]
func (QuestionAndAnswerApi *HealthQAndAApi) UpdateHealthQAndA(c *gin.Context) {
	var QuestionAndAnswer health.HealthQAndA
	err := c.ShouldBindJSON(&QuestionAndAnswer)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Question": {utils.NotEmpty()},
		"Answer":   {utils.NotEmpty()},
		"Author":   {utils.NotEmpty()},
	}
	if err := utils.Verify(QuestionAndAnswer, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := QuestionAndAnswerService.UpdateHealthQAndA(QuestionAndAnswer); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindHealthQAndA 用id查询健康问答表
// @Tags HealthQAndA
// @Summary 用id查询健康问答表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query health.HealthQAndA true "用id查询健康问答表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /QuestionAndAnswer/findHealthQAndA [get]
func (QuestionAndAnswerApi *HealthQAndAApi) FindHealthQAndA(c *gin.Context) {
	var QuestionAndAnswer healthReq.HealthQAndASearchReq
	e := c.ShouldBindQuery(&QuestionAndAnswer)
	if e != nil {
		response.FailWithMessage(e.Error(), c)
		return
	}
	if reQuestionAndAnswer, err := QuestionAndAnswerService.GetHealthQAndA(QuestionAndAnswer.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reQuestionAndAnswer": reQuestionAndAnswer}, c)
	}
}

// GetHealthQAndAList 分页获取健康问答表列表
// @Tags HealthQAndA
// @Summary 分页获取健康问答表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.HealthQAndASearch true "分页获取健康问答表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /QuestionAndAnswer/getHealthQAndAList [get]
func (QuestionAndAnswerApi *HealthQAndAApi) GetHealthQAndAList(c *gin.Context) {
	var pageInfo healthReq.HealthQAndASearchReq
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := QuestionAndAnswerService.GetHealthQAndAInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// GetHealthAnswerListByQuestion 获取回答列表
// @Tags HealthQAndA
// @Summary 获取回答列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.HealthQAndASearch true "获取回答列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /QuestionAndAnswer/getHealthAnswerListByQuestion [get]
func (QuestionAndAnswerApi *HealthQAndAApi) GetHealthAnswerListByQuestion(c *gin.Context) {
	var pageInfo healthReq.HealthQuestionSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	uid := utils.GetUserID(c)
	if list, err := QuestionAndAnswerService.GetHealthAnswerListByQuestion(pageInfo, uid); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}
